package com.zjg.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_notices;
import com.common.entity.A_online;
import com.common.utils.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjg.dao.NoticesDao;
import com.zjg.dao.impl.NoticesDaoImpl;
import com.zjg.dao.impl.OnlineDaoImpl;

public class NoticesServlet extends HttpServlet {
	private NoticesDao noticesDao=new NoticesDaoImpl();
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr=request.getParameter("opr");
		
		if("add".equals(opr)){
			add(request, resp);
		}else if("update".equals(opr))
		{
			update(request,resp);
		}
		else if("del".equals(opr)){
			delete(request, resp);
		}else{
			list(request, resp);
		}
	}
	
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		String url=request.getRequestURL().toString();
		if(url.contains("home.jsp"))
		{
			List<A_notices> list=noticesDao.findByCondition(null);
			ObjectMapper mapper=new ObjectMapper();
			String json=mapper.writeValueAsString(list);
			PrintWriter out=resp.getWriter();
			out.print(json);
		}
		else{
			Map<String, Object> map=new HashMap<String, Object>();
			Integer noticeType_id=StringUtils.str2Integer(request.getParameter("noticeType_id"), null);
			if(noticeType_id!=null) map.put("noticeType_id", noticeType_id);
			List<A_notices> list=noticesDao.findByCondition(map);
			request.setAttribute("noticeslist", list);
			String is_excute_notices=(String) request.getSession().getAttribute("is_excute_notices");
			if(is_excute_notices!=null&&is_excute_notices.equals("yes"))
			{
				request.getSession().setAttribute("is_excute_notices", "no");
				request.getRequestDispatcher("notices_list.jsp").forward(request, resp);
			}
			else
			{
				request.getSession().setAttribute("excuteMsg_notices", "");
				request.getRequestDispatcher("notices_list.jsp").forward(request, resp);
			}
		}
		
//		ObjectMapper mapper=new ObjectMapper();
//		String json=mapper.writeValueAsString(list);
//		
//		PrintWriter out=resp.getWriter();
//		out.print(json);
	}
	
	private void update(HttpServletRequest request, HttpServletResponse resp)
			throws UnsupportedEncodingException, IOException, ServletException {
		//2、判断数据的有效性
		//3、获取数据
		int rows=0;
		String errMsg="";
		try{
			Integer notices_id=StringUtils.str2Integer(request.getParameter("notices_id"), null);
			A_notices notices=null;
			if(notices_id!=null){
				notices=noticesDao.findById(notices_id);
				if(notices==null) throw new RuntimeException("找不到该记录："+notices_id);
				//4、保存数据
				notices.setNoticeType_id(StringUtils.str2Integer(request.getParameter("noticeType_id"), null));
				notices.setTitles(request.getParameter("titles"));
				notices.setContent_msg(request.getParameter("content_msg"));
				notices.setCreate_date(StringUtils.str2TimestampBySql(request.getParameter("create_date"), null, new Timestamp(new Date().getTime()), false));
				rows=noticesDao.updateNotices(notices);
			}else{
				throw new RuntimeException("员工编号必须输入");
			}
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_notices", "yes");
			request.getSession().setAttribute("excuteMsg_notices", "修改成功");
		}else{
			request.getSession().setAttribute("excuteMsg_notices", errMsg);
		}
		resp.sendRedirect("noticesServlet");
	}

	

	private void add(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		//2、判断数据的有效性
		//3、获取数据
		A_notices notices=new A_notices();
		int rows=0;
		String errMsg="";
		try{
			notices.setNoticeType_id(StringUtils.str2Integer(request.getParameter("noticeType_id"), null));
			notices.setTitles(request.getParameter("titles"));
			notices.setContent_msg(request.getParameter("content_msg"));
			notices.setCreate_date(StringUtils.str2TimestampBySql(request.getParameter("create_date"), null, new Timestamp(new Date().getTime()), false));
			//4、保存数据
			Object[] results=(Object[]) noticesDao.saveNotices(notices);
			rows=(Integer) results[0];
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		
		if(rows>0){
			request.getSession().setAttribute("is_excute_notices", "yes");
			request.getSession().setAttribute("excuteMsg_notices", "新增成功");
		}else{
			request.getSession().setAttribute("excuteMsg_notices", errMsg);
		}
		resp.sendRedirect("noticesServlet");
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		int rows=0;
		String errMsg="";
		
		String del_list=request.getParameter("del_list");
		if(del_list!=null&&del_list.length()>0)
		{
			String[] delArrStr=del_list.split(";");
			List<Integer> delArrInt=new ArrayList<Integer>();
			for(int i=0;i<delArrStr.length;i++)
			{
				delArrInt.add(StringUtils.str2Integer(delArrStr[i], null));
			}
			for(Integer i:delArrInt)
			{
				rows=noticesDao.deleteNoticese(i);
			}
		}
		else{
			try{
				Integer notices_id=StringUtils.str2Integer(request.getParameter("notices_id"), null);
				if(notices_id!=null){
					//4、保存数据
					rows=noticesDao.deleteNoticese(notices_id);
				}else{
					throw new RuntimeException("该记录不存在");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
		}	
		if(rows>0){
			request.getSession().setAttribute("is_excute_notices", "yes");
			request.getSession().setAttribute("excuteMsg_notices", "删除成功");
		}else{
			request.getSession().setAttribute("excuteMsg_notices", errMsg);
		}
		resp.sendRedirect("noticesServlet");
		
	}
}
